package GUIMain;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Hashtable;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import profileEditor.ProfileEditor;
import storage.Profile;
import data.ProfileList;

public class ProfileEditorGUI{
	public static JDialog dialog;
	private static JLabel titleLabel;
	private static JLabel profileLabel;
	private static JLabel activeLabel;
	private static JScrollPane profileScrollPane;
	private static JPanel scrollPanel;
	private static JButton newProfileButton;
	private static JButton saveButton;
	private static JButton applyButton;
	private static JButton cancelButton;
	
	//Here we need to pull a copy of the profile list
	public static Hashtable<String, Profile> profileList;
	public static Hashtable<String, Boolean> activeList;
	public static GridBagConstraints gbc;
	public static boolean main;
	
	@SuppressWarnings("unchecked")
	public static void setupGUI(){
		dialog = new JDialog();
		dialog.setTitle("Profile Management");
		titleLabel = new JLabel("Profile Management");
		profileLabel = new JLabel("" + "Profile Name");
		profileLabel.setHorizontalTextPosition(JLabel.LEFT);
		activeLabel = new JLabel("Active");
		scrollPanel = new JPanel();
		profileScrollPane = new JScrollPane(scrollPanel);
		profileScrollPane.setPreferredSize(new Dimension(500,250));
		newProfileButton = new JButton("New Profile");
		saveButton = new JButton("Save and Close");
		applyButton = new JButton("Apply Changes");
		cancelButton = new JButton("Cancel");
		gbc = new GridBagConstraints();
		dialog.setLayout(new GridBagLayout());
		scrollPanel.setLayout(new GridBagLayout());
		gbc.insets.top = 4;
		gbc.insets.bottom = 4;
		gbc.insets.left = 4;
		gbc.insets.right = 4;
		
		JPanel titlePanel = new JPanel();
		titlePanel.add(titleLabel,gbc);
		gbc.gridx = 0;
		gbc.gridy = 0;
		dialog.add(titlePanel,gbc);
		
		gbc.gridx = 0;
		gbc.gridy = 1;
		gbc.gridheight = 3;
		dialog.add(profileScrollPane,gbc);
		
		JPanel buttonPanel = new JPanel(new GridLayout(1,4));
		buttonPanel.add(newProfileButton);
		buttonPanel.add(applyButton);
		buttonPanel.add(saveButton);
		buttonPanel.add(cancelButton);
		gbc.gridheight = 1;
		gbc.gridx = 0;
		gbc.gridy = 4;
		dialog.add(buttonPanel,gbc);
		
		ActionListener newProfileListener = new ActionListener(){
			public void actionPerformed(ActionEvent e){
				ProfileEditor.openEditor();
			}
		};
		newProfileButton.addActionListener(newProfileListener);
		
		ActionListener saveListener = new ActionListener(){
			public void actionPerformed(ActionEvent e){
				ProfileList.profileList = (Hashtable<String, Profile>) profileList.clone();
				for(int i=0;i<activeList.keySet().toArray().length;i++){
					ProfileList.profileList.get(activeList.keySet().toArray()[i]).active = activeList.get(activeList.keySet().toArray()[i]);
				}
				ProfileList.save();
				GUIFocusManager.remove();
			}
		};
		saveButton.addActionListener(saveListener);
		
		ActionListener applyListener = new ActionListener(){
			public void actionPerformed(ActionEvent e){
				ProfileList.profileList = (Hashtable<String, Profile>) profileList.clone();
				for(int i=0;i<activeList.keySet().toArray().length;i++){
					ProfileList.profileList.get(activeList.keySet().toArray()[i]).active = activeList.get(activeList.keySet().toArray()[i]);
				}
				ProfileList.save();
			}
		};
		applyButton.addActionListener(applyListener);
		
		ActionListener cancelListener = new ActionListener(){
			public void actionPerformed(ActionEvent e){
				GUIFocusManager.remove();
			}
		};
		cancelButton.addActionListener(cancelListener);
		
		GUIFocusManager.setDialogProperties(dialog);
	}
	public static void updateProfileList(){
		updateProfileList(null);
	}
	public static void updateProfileList(Profile input){
		if(input!=null){
			//Called from profile editor submit, update temp
			profileList.put(input.profileName, input);
		}
		//Calls this function for this class to reread from the stored list @ ProfileList
		scrollPanel.removeAll();
		Object[] keyList = profileList.keySet().toArray();
		
		for(int i =0;i<keyList.length;i++){
			final Profile p = profileList.get(keyList[i]);
			gbc.gridy = i;
			gbc.gridx = 0;
			p.profileNameLabel.setText((String)keyList[i]);
			p.profileNameLabel.setHorizontalAlignment(JLabel.LEFT);
			scrollPanel.add(p.profileNameLabel,gbc);
			
			gbc.gridx = 1;
			scrollPanel.add(p.editButton,gbc);
			
			gbc.gridx = 2;
			p.activeCheckBox.setSelected(p.active);
			scrollPanel.add(p.activeCheckBox,gbc);
			
			
			//Checking to ensure that a listener is only added once
			//repairs control lockout functionality
			if(p.activeCheckBox.getListeners(ActionListener.class).length<1){
				ActionListener activeListener = new ActionListener(){
					@Override
					public void actionPerformed(ActionEvent arg0) {
						activeList.put(p.profileName, p.activeCheckBox.isSelected());
						//p.active =  p.activeCheckBox.isSelected();
					}
				};
				p.activeCheckBox.addActionListener(activeListener);
			}
			if(p.editButton.getListeners(ActionListener.class).length<1){
				ActionListener editListener = new ActionListener(){
					@Override
					public void actionPerformed(ActionEvent arg0) {
						ProfileEditor.openEditor(p);
					}
				};
				p.editButton.addActionListener(editListener);
			}
		}
		dialog.repaint();
		scrollPanel.revalidate();
	}

	public static boolean deleteProfile(String profileName){
		try{
			profileList.remove(profileName);
			updateProfileList();
			JOptionPane.showMessageDialog(null, "Profile " + profileName + " deletion successful");
			return true;
		}catch(NullPointerException e){
			JOptionPane.showMessageDialog(null, "Profile cannot be deleted (Does not exist yet)");
			return false;
		}
		
	}
	/**
	 * @param main Determines which window is the parent component
	 */
	public static void open(){
		profileList = (Hashtable<String, Profile>) ProfileList.profileList;
		activeList = new Hashtable<String,Boolean>();
		
		updateProfileList();
		GUIFocusManager.add(dialog);
	}
	
}
